package com.med.service.impl;

import com.med.common.ResponseDTO;
import com.med.entity.DepartmentDO;
import com.med.enums.ResponseCode;
import com.med.mapper.DepartmentMapper;
import com.med.service.DepartmentService;
import com.med.vo.AddDepartmentVO;
import com.med.vo.QueryDepartmentVO;
import com.med.vo.UpdateDepartmentVO;
import com.ruoyi.common.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: Internet-based-healthcare
 * @BelongsPackage: com.med.service.impl
 * @Author: 小龚
 * @CreateTime: 2025-08-14 09:03
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

    private final DepartmentMapper departmentMapper;

    private final RedisService redisService;


    @Autowired
    public DepartmentServiceImpl(DepartmentMapper departmentMapper, RedisService redisService) {
        this.departmentMapper = departmentMapper;
        this.redisService = redisService;
    }


    // 查询科室(redis预热、同步Redis、MySQL数据)
    @Override
    public List<DepartmentDO> getDepartmentByPreheat() {
        List<DepartmentDO> list = departmentMapper.getDepartmentByPreheat();
        List<DepartmentDO> tree = buildTree(list);
        redisService.setCacheObject("departmentInfo", tree);
        return tree;
    }

    // 查询科室
    @Override
    public ResponseDTO getDepartment(QueryDepartmentVO vo) {
        List<DepartmentDO> list = departmentMapper.getDepartment(vo);
        List<DepartmentDO> tree = buildTree(list);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("list", tree);
        return ResponseDTO.success(hashMap);
    }


    // 更改科室状态
    @Override
    public ResponseDTO updateStatus(Integer departmentId) {
        Integer count = departmentMapper.updateStatus(departmentId);
        if (count > 0) {
            getDepartmentByPreheat();
            return ResponseDTO.success();
        }
        return ResponseDTO.error(ResponseCode.ERROR);
    }


    // 获取所有父级科室
    @Override
    public ResponseDTO getParentDepartment() {
        List<DepartmentDO> list = departmentMapper.getParentDepartment();
        return ResponseDTO.success(list);
    }


    // 新增科室
    @Override
    public ResponseDTO addDepartment(AddDepartmentVO vo) {
        DepartmentDO department = departmentMapper.judge(vo.getDepartmentName());
        if (department == null) {
            departmentMapper.addDepartment(vo);
            getDepartmentByPreheat();
            return ResponseDTO.success();
        }
        return ResponseDTO.error(ResponseCode.ERROR);
    }


    // 修改科室信息
    @Override
    public ResponseDTO updateDepartmentInfo(UpdateDepartmentVO vo) {
        DepartmentDO department = departmentMapper.judge(vo.getDepartmentName());
        if (department == null) {
            departmentMapper.updateDepartmentInfo(vo);
            getDepartmentByPreheat();
            return ResponseDTO.success();
        }
        return ResponseDTO.error(ResponseCode.ERROR);
    }


    // 删除科室
    @Override
    public ResponseDTO deleteDepartment(Integer departmentId) {

        Integer count = departmentMapper.deleteDepartment(departmentId);
        if (count > 0) {
            getDepartmentByPreheat();
            return ResponseDTO.success();
        }
        return ResponseDTO.error(ResponseCode.ERROR);
    }


    // 构建树形结构
    private List<DepartmentDO> buildTree(List<DepartmentDO> list) {
        List<DepartmentDO> tree = new ArrayList<>();
        Map<Integer, DepartmentDO> map = new HashMap<>();
        // 将科室映射到ID
        for (DepartmentDO departmentDO : list) {
            map.put(departmentDO.getDepartmentId(), departmentDO);
            departmentDO.setChildren(new ArrayList<>());
        }
        // 2. 构建树形结构
        for (DepartmentDO dept : list) {
            Integer parentId = dept.getDepartmentParentId();
            if (parentId == 0) {
                // 顶级科室直接加入
                tree.add(dept);
            } else {
                // 查找父节点
                DepartmentDO parent = map.get(parentId);
                if (parent != null) {
                    // 父节点存在，加入父节点的children
                    parent.getChildren().add(dept);
                } else {
                    // 父节点不存在，作为顶级节点加入（核心修改）
                    tree.add(dept);
                }
            }
        }
        return tree;
    }

}
